﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ToDoList.Entity.Models;

namespace ToDoList.Logic
{
    public class UserSource
    {
        private ToDoListDataContext _ctx;
        public UserSource()
        {
            _ctx = new ToDoListDataContext();
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public bool SignIn(int userId,string passWord)
        {
            var user = TestData.Users.SingleOrDefault(Id => Id.UserId == userId);
            var password = TestData.Users.SingleOrDefault(pw => pw.PassWord.Equals(passWord));
            if(user == null && password == null)
            {
                return false;
            }
            else
                return true;
        }
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="passWord"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public int Register(string passWord, string userName)
        {
            UsersData user = new UsersData()
            {
                UserName = userName,
                Password = passWord
            };
            //_ctx.UsersData
            //_ctx.SaveChanges();
            //return user.UserId;
        }
        /// <summary>
        /// 未完成事件统计
        /// </summary>
        /// <param name="userId"></param>
        public List<WaitToDoList> IncompleteEvent(int userId)
        {
            var WaitToDoLists = TestData.WaitToDoLists.Where(Id => Id.CreationUser == userId)
                .Where(state => state.State == false).ToList<WaitToDoList>();
            return WaitToDoLists;
        }
        /// <summary>
        /// 所有事件浏览
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<WaitToDoList> AllEventQueries(int userId)
        {
            var user = TestData.Users.SingleOrDefault(Id => Id.UserId == userId);
            if(user != null)
            {
                return user.WaitToDoLists.ToList();
            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// 过期事件提醒
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<WaitToDoList> OverdueItems(int userId)
        {
            var user = TestData.Users.SingleOrDefault(Id => Id.UserId == userId);
            var items = user.WaitToDoLists.Where(Time => DateTime.Now > Time.OverdueEvent).ToList();
            if(user != null)
            {
                return items;
            }
            else
            {
                return null;
            }
        }
    }
}
